import os
from PySide6.QtWidgets import QApplication
from PySide6.QtCore import QFile, QTextStream

class StyleManager:
    """样式管理器类，用于加载和应用QSS样式"""
    
    # 预定义样式
    STYLES = {
        "SectionTitle": """
            font-size: 18px;
            font-weight: bold;
            color: #1565C0;
            margin-bottom: 12px;
            padding-left: 5px;
            border-left: 4px solid #2196F3;
        """,
        "StyledFrame": """
            background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                               stop:0 #FFFFFF, stop:1 #F5F5F5);
            border-radius: 10px;
            border: 1px solid #E0E0E0;
            padding: 12px;
        """,
        "StyledPanel": """
            background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, 
                               stop:0 #FFFFFF, stop:1 #FAFAFA);
            border-radius: 8px;
            border: 1px solid #E0E0E0;
            padding: 10px;
        """,
        "coordCombo": """
            background-color: #FFFFFF;
            border: 1px solid #BDBDBD;
            border-radius: 5px;
            padding: 6px;
            color: #424242;
            font-weight: bold;
        """,
        "imagePreview": """
            border: 1px solid #BDBDBD;
            border-radius: 8px;
            background-color: #FAFAFA;
        """
    }
    
    @staticmethod
    def update_style_class(style_name, style_definition):
        """更新或添加样式类定义"""
        if not style_name:
            return False
        StyleManager.STYLES[style_name] = style_definition
        return True
    
    @staticmethod
    def load_style_sheet(file_path):
        """从QSS文件加载样式表"""
        if not os.path.exists(file_path):
            StyleManager.debug_print(f"样式文件不存在: {file_path}")
            return ""
            
        try:
            with QFile(file_path) as qss_file:
                if not qss_file.open(QFile.ReadOnly | QFile.Text):
                    StyleManager.debug_print(f"无法打开样式文件: {file_path}")
                    return ""
                    
                stream = QTextStream(qss_file)
                style_sheet = stream.readAll()
                return style_sheet
        except Exception as e:
            StyleManager.debug_print(f"读取样式文件时出错: {e}")
            return ""
    
    @staticmethod
    def apply_style(widget, style_name=None, object_name=None):
        """应用预定义样式到控件"""
        if object_name:
            widget.setObjectName(object_name)
            if object_name in StyleManager.STYLES:
                widget.setStyleSheet(StyleManager.STYLES[object_name])
        
        if style_name and style_name in StyleManager.STYLES:
            widget.setStyleSheet(StyleManager.STYLES[style_name])
        
        widget.style().unpolish(widget)
        widget.style().polish(widget)
    
    @staticmethod
    def apply_global_style(app, style_file_path):
        """应用全局样式到应用程序"""
        style_sheet = StyleManager.load_style_sheet(style_file_path)
        if style_sheet:
            app.setStyleSheet(style_sheet)
            return True
        return False

    @staticmethod
    def load_style(file_path):
        """加载样式文件内容"""
        style_content = ""
        try:
            if not os.path.exists(file_path):
                StyleManager.debug_print(f"样式文件不存在: {file_path}")
                return style_content
            
            try:
                with open(file_path, "r", encoding="utf-8") as file:
                    style_content = file.read()
                return style_content
            except Exception as e:
                StyleManager.debug_print(f"无法打开样式文件: {file_path}")
                return style_content
                
        except Exception as e:
            StyleManager.debug_print(f"读取样式文件时出错: {e}")
            return style_content
            
    @staticmethod
    def get_style(style_name):
        """获取指定样式的定义"""
        return StyleManager.STYLES.get(style_name, "")
    
    @staticmethod
    def debug_print(message):
        """统一处理打印调试信息的方法"""
        try:
            # 导入配置
            import sys
            import os
            # 当前文件所在的目录路径
            current_dir = os.path.dirname(os.path.abspath(__file__))
            # 项目根目录路径
            root_dir = os.path.abspath(os.path.join(current_dir, ".."))
            # 将项目根目录添加到sys.path
            if root_dir not in sys.path:
                sys.path.insert(0, root_dir)
                
            from config.settings import ENABLE_PRINT
            
            if ENABLE_PRINT:
                print(message)
        except ImportError:
            # 如果导入失败，默认打印
            print(message) 